package com.jbl.te.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.MD5;
import com.jbl.ma.entity.UserApply;
import com.jbl.ma.entity.UserApplyEvidence;
import com.jbl.ma.service.UserApplyEvidenceService;
import com.jbl.ma.service.UserApplyService;
import com.jbl.ma.vo.TeacherUserVO;
import com.jbl.te.entity.College;
import com.jbl.te.entity.Courseclass;
import com.jbl.te.entity.Teacher;
import com.jbl.te.entity.User;
import com.jbl.te.entity.dto.TeacherQuery;
import com.jbl.te.mapper.TeacherMapper;
import com.jbl.te.service.CollegeService;
import com.jbl.te.service.CourseclassService;
import com.jbl.te.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbl.te.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-10
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private CourseclassService courseclassService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserApplyService applyService;
    @Autowired
    private UserApplyEvidenceService evidenceService;

    @Override
    public boolean updateTeacher(Teacher teacher) {
        StringBuilder number = new StringBuilder(); //拼接教工号

        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
        number.append(sformat.format(teacher.getHiredate()).substring(0,4)); //入职年份前4号
        //院序号，2位
        List<College> collegeList = collegeService.list();
        for (Integer i = 0;i < collegeList.size();i++) {
            if(collegeList.get(i).getId().equals(teacher.getCollegeId())) {
                if(i < 10) {
                    number.append("0");
                } number.append(i.toString());
            }
        }
        number.append(teacher.getLevel().toString()); //level，1位
        //教师序号，3位
        Integer count = teacher.getOrderNumber();
        if (count < 10) number.append("00");
        else if(count < 100) number.append("0");
        number.append(count.toString());

        teacher.setTeacherNumber(number.toString());
        return this.updateById(teacher);
    }

    @Transactional
    @Override
    public String saveTeacher(TeacherUserVO teacherUserVO) {

        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherUserVO,teacher);

        StringBuilder number = new StringBuilder(); //拼接教工号

        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
        number.append(sformat.format(teacher.getHiredate()).substring(0,4)); //入职年份前4号
        //院序号，2位
        List<College> collegeList = collegeService.list();
        for (Integer i = 0;i < collegeList.size();i++) {
            if(collegeList.get(i).getId().equals(teacher.getCollegeId())) {
                if(i < 10) {
                    number.append("0");
                } number.append(i.toString());
            }
        }
        number.append(teacher.getLevel().toString()); //level，1位
        //教师序号，3位
        teacher.setOrderNumber(this.count());
        Integer count = teacher.getOrderNumber();
        if (count < 10) number.append("00");
        else if(count() < 100) number.append("0");
        number.append(count.toString());

        teacher.setTeacherNumber(number.toString());
        this.save(teacher);

        //添加用户
        User user = new User();
        user.setId(teacher.getId());
        user.setName("教师注册用户");
        user.setRole("common_teacher");
        user.setUsername(teacher.getTeacherNumber());
        String psd = teacher.getIdentityNumber().substring(teacher.getIdentityNumber().length()-8);
        user.setPassword(MD5.encrypt(psd)); //密码为身份证后8位（加密）
        userService.save(user);

        //添加管理员审核
        UserApply userApply = new UserApply();
        userApply.setTeacherNumber(teacher.getId());
        applyService.save(userApply);
        UserApplyEvidence evidence = new UserApplyEvidence();
        evidence.setApplyId(userApply.getApplyId());
        evidence.setContent("麻烦管理员审核一下");
        evidence.setRole(teacherUserVO.getRole());
        evidenceService.save(evidence);

        return teacher.getTeacherNumber();
    }

    @Override
    public void pageQuery(Page<Teacher> pageParam, TeacherQuery teacherQuery) {
        if (teacherQuery == null) {
            this.page(pageParam,null);
            return;
        }
        String name = teacherQuery.getName();
        String collegeId = teacherQuery.getCollegeId();
        Integer political = teacherQuery.getPolitical();
        String title = teacherQuery.getTitle();
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(collegeId)) {
            wrapper.eq("college_id",collegeId);
        }
        if(!StringUtils.isEmpty(political)) {
            wrapper.eq("political",political);
        }
        if(!StringUtils.isEmpty(title)) {
            wrapper.eq("title",title);
        }

        this.page(pageParam,wrapper);
    }

    @Transactional
    @Override
    public boolean removeTeacherById(String id) {
        QueryWrapper<Courseclass> courseclassQueryWrapper = new QueryWrapper<>();
        courseclassQueryWrapper.eq("teacher_id",id);
        courseclassService.remove(courseclassQueryWrapper);
        userService.removeById(id);
        applyService.remove(new LambdaQueryWrapper<UserApply>().eq(UserApply::getTeacherNumber,id));
        return this.removeById(id);
    }
}
